---
layout: "default"
title: "Hashable"
description: "Swift documentation for 'Hashable': A type that provides an integer hash value."
keywords: "Hashable,protocol,swift,documentation,==,hashValue"
root: "/v3.0"
---

<div class="intro-declaration"><code class="language-swift">protocol Hashable</code></div>

<div class="discussion comment">
    <p>A type that provides an integer hash value.</p>

<p>You can use any type that conforms to the <code>Hashable</code> protocol in a set or
as a dictionary key. Many types in the standard library conform to
<code>Hashable</code>: strings, integers, floating-point and Boolean values, and even
sets provide a hash value by default. Your own custom types can be
hashable as well. When you define an enumeration without associated
values, it gains <code>Hashable</code> conformance automatically, and you can add
<code>Hashable</code> conformance to your other custom types by adding a single
<code>hashValue</code> property.</p>

<p>A hash value, provided by a type&#39;s <code>hashValue</code> property, is an integer that
is the same for any two instances that compare equally. That is, for two
instances <code>a</code> and <code>b</code> of the same type, if <code>a == b</code> then
<code>a.hashValue == b.hashValue</code>. The reverse is not true: Two instances with
equal hash values are not necessarily equal to each other.</p>

<p><strong>Important:</strong> Hash values are not guaranteed to be equal across different
  executions of your program. Do not save hash values to use during a
  future execution.</p>

<h1>Conforming to the Hashable Protocol</h1>

<p>To use your own custom type in a set or as the key type of a dictionary,
add <code>Hashable</code> conformance to your type by providing a <code>hashValue</code>
property. The <code>Hashable</code> protocol inherits from the <code>Equatable</code> protocol,
so you must also add an equal-to operator (<code>==</code>) function for your
custom type.</p>

<p>As an example, consider a <code>GridPoint</code> type that describes a location in a
grid of buttons. Here&#39;s the initial declaration of the <code>GridPoint</code> type:</p>

<pre><code class="language-swift">/// A point in an x-y coordinate system.
struct GridPoint {
    var x: Int
    var y: Int
}</code></pre>

<p>You&#39;d like to create a set of the grid points where a user has already
tapped. Because the <code>GridPoint</code> type is not hashable yet, it can&#39;t be used
as the <code>Element</code> type for a set. To add <code>Hashable</code> conformance, provide an
<code>==</code> operator function and a <code>hashValue</code> property.</p>

<pre><code class="language-swift">extension GridPoint: Hashable {
    var hashValue: Int {
        return x.hashValue ^ y.hashValue
    }

    static func == (lhs: GridPoint, rhs: GridPoint) -&gt; Bool {
        return lhs.x == rhs.x &amp;&amp; lhs.y == rhs.y
    }
}</code></pre>

<p>The <code>hashValue</code> property in this example combines the hash values of a grid
point&#39;s <code>x</code> and <code>y</code> values using the bitwise XOR operator (<code>^</code>). The <code>^</code>
operator is one way to combine two integer values into a single value.</p>

<p><strong>Note:</strong> Set and dictionary performance depends on hash values that minimize
  collisions for their associated element and key types, respectively.</p>

<p>Now that <code>GridPoint</code> conforms to the <code>Hashable</code> protocol, you can create a
set of previously tapped grid points.</p>

<pre><code class="language-swift">var tappedPoints: Set = [GridPoint(x: 2, y: 3), GridPoint(x: 4, y: 1)]
let nextTap = GridPoint(x: 0, y: 1)
if tappedPoints.contains(nextTap) {
    print(&quot;Already tapped at (\(nextTap.x), \(nextTap.y)).&quot;)
} else {
    tappedPoints.insert(nextTap)
    print(&quot;New tap detected at (\(nextTap.x), \(nextTap.y)).&quot;)
}
// Prints &quot;New tap detected at (0, 1).&quot;)</code></pre>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">Equatable</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>



<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>




<h3>Instance Variables</h3>
<div class="declaration" id="var-hashvalue_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-hashvalue_-int">var hashValue: Int</a> <span class="required">Required</span><div class="comment collapse" id="comment-var-hashvalue_-int"><div class="p">
    <p>The hash value.</p>

<p>Hash values are not guaranteed to be equal across different executions of
your program. Do not save hash values to use during a future execution.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var hashValue: Int { get }</code>

    </div></div>
</div>



<h3>Instance Methods</h3>
<div class="declaration inherited" id="func-eqeq_rhs_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-eqeq_rhs_">func ==(<wbr>_:<wbr>rhs:)</a>
     <span class="required">Required</span>    
<div class="comment collapse" id="comment-func-eqeq_rhs_"><div class="p">
    <p>Returns a Boolean value indicating whether two values are equal.</p>

<p>Equality is the inverse of inequality. For any values <code>a</code> and <code>b</code>,
<code>a == b</code> implies that <code>a != b</code> is <code>false</code>.</p>

<p><strong>Parameters:</strong>
  <strong>lhs:</strong> A value to compare.
  <strong>rhs:</strong> Another value to compare.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func ==(lhs: Self, rhs: Self) -&gt; Bool</code>
    
        <h4>Declared In</h4>
        <a href="../../protocol/Equatable/"><code>Equatable</code></a>    
</div></div>
</div>


